const axios = require('axios').default;
const iconv = require('iconv-lite')
async function getHtml(url) {
    /* 
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    获取gbk格式的网页，需要通过接口已流的形式请求，需要通过iconv包提供的decode方法，拿到正确的utf-8格式的字符串
    */
    let res = await axios({
        url,
        responseType: 'stream' // 以数据流的方式输出
    })
    // 返回一个promise实例对象
    return new Promise(resolve => {
        const chunks = []
        res.data.on('data', chunk => {
            chunks.push(chunk)
        })
        res.data.on('end', () => {
            const buffer = Buffer.concat(chunks)
            const str = iconv.decode(buffer, 'gbk')
            resolve(str)
        })
    })
}
module.exports = getHtml
